package org.huangxiaozhe.adventure;

import org.huangxiaozhe.adventure.common.ConstantUtil;
import org.huangxiaozhe.adventure.common.RoadPoint;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.triplefstudio.adventure.R;


public class Road {
	
	private int BaseY;
	private final static int roadHL = 20;	//·��ˮƽ��϶
	private final static int roadVL = 10;	//·�Ĵ�ֱ��϶
	private  static int minY;
	private RoadPoint[] points;
	private int point_s, point_e;	//��ǰ����Ƶĵ����ֹ����
	private int point_count;
	private boolean start = true;
	private static Bitmap bitmapInsect;
	private static Bitmap bitmapSting;
	private GameView gameView;
	
	public Road(int y, GameView gameView){
		this.gameView = gameView;
		this.BaseY = y;
		minY = (BaseY - ConstantUtil.screenHeight)/roadVL;
		
		bitmapInsect =  BitmapFactory.decodeResource(this.gameView.getResources(), R.drawable.insect);
		bitmapSting =  BitmapFactory.decodeResource(this.gameView.getResources(), R.drawable.sting);
		
		int count = ConstantUtil.screenWidth / roadHL + 1;
		point_count = 300;
		points = new RoadPoint[point_count];
		point_s = 0;
		point_e = count;
		for(int i=point_s; i<=point_e; i++)
		{
			points[i] = new RoadPoint();
			points[i].x = roadHL * (i - point_s);
		}
		for(int i=point_e + 1; i<point_count; i++)
		{
			points[i] = new RoadPoint();
		}
		
		for(int i = 30; i< 40;i+=2){
			points[i].insect = true;
			points[i].insectY = 8 + i%10;
		}
		for(int i = 30; i< 40;i++){
			points[i].y = 2;
			points[i].bgY = 2;
		}
		for(int i = 40; i< 48;i++){
			points[i].y = 3;
			points[i].bgY = 3;
			points[i].sting = true;
		}
		for(int i = 50; i< 60;i++){
			points[i].tpye = RoadPoint.RiverType;
			points[i].y = minY;
		}
		for(int i =66; i<70; i++){
			points[i].tpye = RoadPoint.NullType;
			points[i].y = minY;
		}
		points[81].y = points[81].bgY = 3;
		points[82].y = points[82].bgY = 4;
		points[83].y = points[83].bgY = 5;
		points[84].y = points[84].bgY = 6;
		points[85].y = points[85].bgY = 7;
		points[90].y = points[90].bgY = 10;
		points[91].y = points[91].bgY = 15;
		points[92].y = points[92].bgY = 20;
		
	}
	
	public void draw(Canvas canvas){
		Paint paint = new Paint();
		
		for(int i=point_s; i<point_e; i++){
			//����·��
			if(points[i].tpye == RoadPoint.RoadType){
				paint.setColor(Color.RED);
				canvas.drawRect(points[i].x, BaseY - points[i].bgY * roadVL, points[i + 1].x, ConstantUtil.screenHeight, paint);
			}
			else if(points[i].tpye == RoadPoint.RiverType){
				paint.setColor(Color.BLUE);
				canvas.drawRect(points[i].x, BaseY - points[i].bgY * roadVL, points[i + 1].x, ConstantUtil.screenHeight, paint);
			}
			else if(points[i].tpye == RoadPoint.NullType){
				//paint.setColor(Color.BLUE);
				//canvas.drawRect(points[i].x, BaseY - points[i].y * roadVL, points[i + 1].x, ConstantUtil.screenHeight, paint);
			}
			
			//���ƿ��зɳ�
			if(points[i].insect == true){
				canvas.drawBitmap(bitmapInsect, points[i].x, BaseY - points[i].insectY * roadVL, paint);
			}
			
			//���Ƶش�
			if(points[i].sting == true){
				canvas.drawBitmap(bitmapSting, points[i].x, BaseY - points[i].bgY * roadVL - bitmapSting.getHeight(), paint);
			}
			
			//���Ʊ߽�debug
			paint.setColor(Color.BLACK);
			canvas.drawRect(points[i].x, BaseY - points[i].y * roadVL, points[i + 1].x, BaseY - points[i].y * roadVL + 4, paint);
			if(points[i+1].tpye == RoadPoint.RoadType){
				if(points[i+1].y > points[i].y){
					canvas.drawRect(points[i + 1].x - 4, BaseY - points[i].y * roadVL, points[i + 1].x, BaseY - points[i + 1].y * roadVL, paint);
				}
				else{
					canvas.drawRect(points[i + 1].x - 4, BaseY - points[i].y * roadVL, points[i + 1].x, BaseY - points[i + 1].y * roadVL + 4, paint);
				}
			}
			paint.setColor(Color.GRAY);
			canvas.drawCircle(points[i].x, BaseY - points[i].y * roadVL, 2, paint);
			canvas.drawCircle(points[i].x + roadHL, BaseY - points[i].y * roadVL, 2, paint);
		}
	}

	//�ú�����������·��ƽ��
	public void Move() {
		if(!start){
			return;
		}
		if(point_e >= point_count - 2){
			return;
		}
		String s = "draw(" + (point_e - point_s) + "):";
		
		for(int i=point_s; i<=point_e; i++){
			s+= i + "(" + points[i].x + "," + points[i].y + ") ";
			points[i].x -= ConstantUtil.roadSpan;
		}
		//��ǰ�ĵ��Ƴ���Ļ�Ҳ�Ӱ���ͼ���޸���ʼ����
		while(points[point_s].x <= -roadHL){
			point_s ++;
		}
		//����������Ļ���޸Ľ�������
		while(points[point_e].x < ConstantUtil.screenWidth){
			//Log.d(ConstantUtil.tag, point_e + " " + points[point_e].x);
			point_e ++;
			points[point_e].x = points[point_e-1].x + roadHL;
		}
		//Log.d(ConstantUtil.tag, s);
	}
	
	//��ݺ����ȡ��·���ϵ������
	public float getY(float x){
		float ret = BaseY;
		int i;
		for(i = point_s; i <= point_e; i++){
			if(points[i].x == x){
				ret =  BaseY - roadVL * points[i].y;
				break;
			}
			else if(points[i].x > x){
				ret =  BaseY - roadVL * points[i-1].y;
				break;
			}
		}
		return ret;
	}
	
	//������������ȡ����һ������ߵ������
	public float getHorizon(float x1, float x2){
		float ret = 0;
		if(x2 < x1){
			float t = x2;
			x2 = x1;
			x1 = t;
		}
		int compare1;
		for(compare1 = point_s; compare1 <= point_e; compare1++){
			if(points[compare1].x == x1){
				ret = points[compare1].y;
				break;
			}
			else if(points[compare1].x > x1){
				ret = points[compare1 - 1].y;
				break;
			}
		}
		
		int compare2;
		for(compare2 = compare1; compare2 < point_e; compare2++){
			if(points[compare2].x <= x2){
				if(points[compare2].y > ret){
					ret = points[compare2].y;
				}
			}
			else if(points[compare2].x > x2){
				break;
			}
		}
		
		return BaseY - ret * roadVL;
	}
	
	public void Start(){
		this.start = true;
	}
	
	public void Stop(){
		this.start = false;
	}
}
